package net.wanho.shop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.RequiredArgsConstructor;
import net.wanho.shop.entity.Detail;
import net.wanho.shop.entity.Orders;
import net.wanho.shop.mapper.DetailMapper;
import net.wanho.shop.mapper.OrdersMapper;
import net.wanho.shop.service.OrdersService;
import net.wanho.shop.vo.DetailVo;
import net.wanho.shop.vo.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2025-09-24 13:51
 * Description：<描述>
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class OrdersServiceImpl implements OrdersService {

    private final OrdersMapper ordersMapper;
    private final DetailMapper detailMapper;

    @Override
    public Page<Orders> findPage(Integer pageNum, Integer pageSize, Orders orders) {
        PageHelper.startPage(pageNum, pageSize);

        List<Orders> list = ordersMapper.selectList(orders);

        PageInfo<Orders> pageInfo = new PageInfo<>(list);

        Page<Orders> page = new Page<>(pageInfo.getTotal(), pageInfo.getList());


        // 为每个订单添加订单明细
        page.getRecords().forEach(item -> {
            List<DetailVo> details = detailMapper.selectByOrdersId(item.getId());
            item.setDetails(details);
        });

        return page;
    }
}
